package com.dycong.common.leetcode.yi;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-07-16
 * Time: 11:08
 */
public class MaxLargestL {

    public static void main(String[] args) {
        System.out.println(longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
    }

    public static int longestConsecutive(int[] nums) {
        int max = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i : nums) {
            if (map.getOrDefault(i, 0) == 0) {
                int left = map.getOrDefault(i - 1, 0);
                int right = map.getOrDefault(i + 1, 0);
                map.put(i, left + right + 1);
                if (left != 0) {
                    map.put(i - left, left + right + 1);
                }
                if (right != 0) {
                    map.put(i + right, left + right + 1);
                }
                max = max > (left + right + 1) ? max : left + right + 1;
            }
        }
        return max;
    }
}
